﻿ko.bindingHandlers.href = {
    init: function (element, valueAccessor) {
        var value = ko.utils.unwrapObservable(valueAccessor());
        $(element).attr('href', value);
    },
    update: function (element, valueAccessor) {
        var value = ko.utils.unwrapObservable(valueAccessor());
        $(element).attr('href', value);
    }
};

ko.bindingHandlers.title = {
    init: function (element, valueAccessor) {
        var value = ko.utils.unwrapObservable(valueAccessor());
        $(element).attr('title', value);
    },
    update: function (element, valueAccessor) {
        var value = ko.utils.unwrapObservable(valueAccessor());
        $(element).attr('title', value);
    }
};

ko.bindingHandlers.enterPressed = {
    init: function (element, valueAccessor) {
        var callback = valueAccessor();
        $(element).bind('keypress', function (e) {
            if (e.which == 13) {
                setTimeout(function () {
                    if (callback) callback();
                }, 5);
            }
        });
    }
};

ko.bindingHandlers.instantValue = {
    init: function (element, valueAccessor) {
        var $element = $(element);
        var accessor = valueAccessor();
        $element.val(accessor());
        $element.changeWatcher().reset();
        $element.changeWatcher().bind(function (ev, val) {
            accessor(val);
        });
    },
    update: function (element, valueAccessor) {
        var $element = $(element);
        var val = valueAccessor()();
        $element.changeWatcher().reset();
        if ($element.val() != val) {
            $element.val(val);
            $element.trigger('change');
        }
    }
};


ko.bindingHandlers.modalSuccess = {
    init: function (element, valueAccessor) {
        var val = valueAccessor();
        var $dlg = $(element);
        val(function () {
            $dlg.reveal2().close();
        });
    }
};


ko.bindingHandlers.modalOpen = {
    init: function (element, valueAccessor) {
        var dlgId = valueAccessor();
        $(element).click(function () {
            ko.bindingHandlers.modalOpen.show(dlgId);
        });
    },
    show: function (dlgId) {
        var $dlg = $('#' + dlgId);
        $dlg.reveal2().open({
            closeOnBackgroundClick: true,
            opened: function () {
                $dlg.find('.enter-focus').focus();
            }
        });
    }
};

ko.bindingHandlers.hover = {
    init: function (element, valueAccessor) {
        var callback = valueAccessor();
        $(element).bind('mouseover', function () {
            callback();
        });
    }
};

ko.bindingHandlers.mruOptions = {
    init: function (element, valueAccessor) {
        var value = ko.utils.unwrapObservable(valueAccessor());
        $(element).mruList().setItems(value);
    },
    update: function (element, valueAccessor) {
        var value = ko.utils.unwrapObservable(valueAccessor());
        $(element).mruList().setItems(value);
    }
};

ko.bindingHandlers.mruSelect = {
    init: function (element, valueAccessor) {
        var value = ko.utils.unwrapObservable(valueAccessor());
        $(element).mruList().addOnSelect(value);
    }
};

(function () {

    function refresh(element, spinner) {
        var $progressZone = $(element).progressZone();
        $progressZone.set(spinner.loading());
    }

    ko.bindingHandlers.spinner = {
        update: function (element, valueAccessor) {
            var spinner = ko.utils.unwrapObservable(valueAccessor());

            function onValueChanged() {
                refresh(element, spinner);
            }

            //TODO: unsubscribe form prevoius spinner model.. rare case actally
            spinner.addChanged(onValueChanged);

            refresh(element, spinner);
        }
    };
})();